package ru.neoflex.jedit.plugin;

import org.apache.commons.lang3.StringUtils;
import org.gjt.sp.jedit.*;
import org.gjt.sp.jedit.textarea.TextArea;
import org.gjt.sp.util.Log;
import ru.neoflex.jedit.plugin.core.NeoflexState;

import javax.swing.*;
import javax.swing.event.CaretListener;
import java.awt.*;
import java.io.IOException;

/**
 * Плагин для работы с Neoflex FO.
 * Умеет показыват подсказку и делать autocompletion по xhtml тэгам.
 */
public class NeoflexPlugin extends EBPlugin {
    public static final String PROPS_PREFIX = "plugin.ru.neoflex.jedit.plugin.NeoflexPlugin.";

    public NeoflexPlugin() {
        Log.log(Log.NOTICE, this, "NeoflexPlugin()");
    }

    static {
        Log.init(true, Log.NOTICE);
        Log.log(Log.NOTICE, null, "static NeoflexPlugin()");
    }

    public void start() {
        super.start();
        EditBus.addToBus(this);

        Log.log(Log.NOTICE, this, "Neoflex plugin started");

        registedCaretListener();
    }

    public void stop() {
        EditBus.removeFromBus(this);

        Log.log(Log.NOTICE, this, "Neoflex plugin stopped");

        unRegisterCaretListener();
    }

    public void handleMessage(EBMessage ebMessage) {
        //  nothing
    }

    //
    //  JEDIT SYSTEM PART
    //

    /**
     * Зарегистрировать обработчик изменеия положения каретки для WhisdomBar.
     */
    private static void registedCaretListener() {
        TextArea textArea = jEdit.getActiveView().getEditPane().getTextArea();
        CaretListener caretListener = NeoflexState.getInstance().getHhtmlCaretListener();
        textArea.addCaretListener(caretListener);
        Log.log(Log.NOTICE, null, "Caret listener registered");
    }

    /**
     * Отрегистрировать обработчик изменеия положения каретки для WhisdomBar.
     */
    private static void unRegisterCaretListener() {
        TextArea textArea = jEdit.getActiveView().getEditPane().getTextArea();
        CaretListener caretListener = NeoflexState.getInstance().getHhtmlCaretListener();
        textArea.removeCaretListener(caretListener);
        Log.log(Log.NOTICE, null, "Caret listener unregistered");
    }


    //
    //  ACTIONS
    //

    public static void sayHelloWorld() {
    }

    public static void loadProjectSettings() {
        // nothing
    }

    public static void showProjectSettings() {
        // nothing
    }

    public static void loadXhtmlAutocompleteSettings() throws IOException {
        Log.log(Log.NOTICE, null, "Loading xhtml autocomplete settings");
        NeoflexState.getInstance().reloadXhtmlAutocompleteSettings();
        Log.log(Log.NOTICE, null, "Loaded tags: " + NeoflexState.getInstance().getXhtmlAutocompleteSettings().getTags().size());
        Log.log(Log.NOTICE, null, StringUtils.join(NeoflexState.getInstance().getXhtmlAutocompleteSettings().getTags(), ", "));
    }

    public static void showXhtmlAutocompleteSettings() {
        // Stub implementation writes info to log
        if (NeoflexState.getInstance().getXhtmlAutocompleteSettings() != null) {
            Log.log(Log.NOTICE, null, "Xhtml tags loaded: " + NeoflexState.getInstance().getXhtmlAutocompleteSettings().getTags().size());
            Log.log(Log.NOTICE, null, StringUtils.join(NeoflexState.getInstance().getXhtmlAutocompleteSettings().getTags(), ", "));
        } else {
            Log.log(Log.NOTICE, null, "Xhtml tags NOT loaded!");
        }
    }

    public static void showXhtmlAutocompletePopup() {
        String modeName = jEdit.getActiveView().getBuffer().getMode().getName();
        if (modeName.equals("xml") || modeName.equals("xhtml") || modeName.equals("html")) {
            NeoflexState.getInstance().showXhtmlAutocompletePopup();
        }
    }

    public static void rebuildPropertiesFileIndex() {
        NeoflexState.getInstance().rebuildPropertiesFileIndex();
    }

    public static void main(String[] args) throws IOException {
        loadXhtmlAutocompleteSettings();
    }

}
